package com.wang.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Demo {


    public static void main(String[] args) {

        String datas[] = {"03","03","03","03","03","03","03","03"};

        StringBuilder sbData  = new StringBuilder();
        StringBuilder sbCrc  = new StringBuilder();

        for (int j = 0; j < datas.length; j++) {
            if (j < datas.length - 2) {
                sbData.append(datas[j]);
            } else {
                sbCrc.append(datas[j]);
            }
        }

        System.out.println(sbData.toString());
        System.out.println(sbCrc.toString());



    }

    /**
     * 将小端bytes数据转化为大端数据
     * <p>
     * 默认网络传输字节为大端，java 全部为大端（与平台无关）
     * 关于 “Little-Endian and Big-Endian”,详情请参考：
     *
     * @param bytes
     * @return 转化后得到的整数
     * @Link https://howtodoinjava.com/java/basics/little-endian-and-big-endian-in-java/
     * </p>
     */
    private static void bytesToBigEndian(byte[] bytes) {
        String hex = "5A109061"; // mEditText.getText().toString()

// Parse hex to int
        int value = Integer.parseInt(hex, 16);

// Flip byte order using ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.order(ByteOrder.BIG_ENDIAN);
        buffer.asIntBuffer().put(value);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        int flipped = buffer.asIntBuffer().get();

        System.out.println("hex: 0x" + hex);
        System.out.println("flipped: " + flipped);
    }
}
